from datetime import datetime
import datetime as dt
from django.http import JsonResponse
from django.shortcuts import render
from django.utils import timezone
from Book_exaln_system.models import Examination, ExaminationForPeople
from djangoProject1.settings import get_week, update_exaln


def index(request):
    date = timezone.now()
    week_now = get_week()
    print(update_exaln,date.day,week_now)
    if update_exaln[0] or (update_exaln[1] != date.day) or Examination.objects.filter(examination_time__lt=date):
        if week_now in (6, 7):
            for i in range(5):
                # sbj 1,4
                for sbj in (1, 4):
                    for t in (9, 10, 13, 15):
                        day = (date.day + (week_now % 7) + 1 +i)
                        Examination.objects.create(
                            examination_time=datetime.strptime(
                                str(date.year) + '-' + str(date.month) + '-' + str(date.day) + ' %d:00:00' % t,
                                '%Y-%m-%d %H:%M:%S')+dt.timedelta(hours=24*day),
                            examination_type=sbj,
                            number_of_people=100
                        )
                # sbj 2 3
                for sbj in (2, 3):
                    for t in (8, 13):
                        day = (date.day + (week_now % 7) + 1 + i)
                        Examination.objects.create(
                            examination_time=datetime.strptime(
                                str(date.year) + '-' + str(date.month) + '-' + str(date.day) +' %d:00:00'%t,
                                '%Y-%m-%d %H:%M:%S')+dt.timedelta(hours=24*day),
                            examination_type=sbj,
                            number_of_people=100
                        )

        else:
            for i in Examination.objects.filter(time_out=False):
                if i.examination_time < timezone.now():
                    i.time_out = True
                    i.save()
        update_exaln[1] = date.day
        update_exaln[0] = True

    query_1 = Examination.objects.filter(examination_type=1,time_out=False).all().order_by('examination_time')
    query_2 = Examination.objects.filter(examination_type=2,time_out=False).all().order_by('examination_time')
    query_3 = Examination.objects.filter(examination_type=3,time_out=False).all().order_by('examination_time')
    query_4 = Examination.objects.filter(examination_type=4,time_out=False).all().order_by('examination_time')
    return render(request, 'book_exaln/index.html',
                  context={'data': [query_1, query_2, query_3, query_4], 'datenow': date})


def book(req):
    if not req.user.is_authenticated:
        return JsonResponse({'error': 'Please login in  '})
    try:
        exa = ExaminationForPeople.objects.filter(student=req.user, exaln__examination_type=Examination.objects.filter(
            id=req.POST.get('id')).first().examination_type).first()
        if exa:
            raise Exception('you has an appointment.')
        else:
            ExaminationForPeople.objects.create(
                student=req.user,
                exaln=Examination.objects.filter(id=req.POST.get('id')).first(),
            )
            return JsonResponse({'error': None, 'data': 'book successfully '})
    except Exception as e:
        return JsonResponse({'error': str(e)})
